home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / hypercar / xcmd / dartxcmd.sit / Dartmouth XCMD's 3.1 / card_19348.txt < prev    next >
Encoding:
Text File  |  1989-06-02  |  13.4 KB  |  495 lines

  1. -- card: 19348 from stack: in.1
  2. -- bmap block id: 19513
  3. -- flags: 0000
  4. -- background id: 8327
  5. -- name: FindInField
  6. ----- HyperTalk script -----
  7. on Install
  8.   put ChooseTargetStack() into it
  9.   InstallResource XFCN,FindInField,it
  10. end Install
  11.  
  12. on openCard
  13.   set the hilite of btn "foundit" to false
  14. end openCard
  15.  
  16. on showIt pos,theLength
  17.   set the hilite of btn "foundit" to false
  18.   if item 1 of pos + item 2 of pos + item 3 of pos + item 4 of pos = 0 then
  19.     answer "Target not found!"
  20.     exit ShowIt
  21.   end if
  22.   put item 1 of the rect of card field 1 into x
  23.   put item 2 of the rect of card field 1 into y
  24.   put item 3 of pos into theChar
  25.   put item 2 of pos into theLine
  26.   put the textHeight of card field 1 into th
  27.   put 6 into ts  -- courier
  28.   put y+(theLine*th) into y
  29.   put the scroll of card field 1 into theScroll
  30.   get the rect of card field 1
  31.   put item 4 of it - item 2 of it into theHeight
  32.   if (theScroll+theHeight) < y then
  33.     set the scroll of card field 1 to (y-10*th)
  34.   end if
  35.   put x+(theChar*ts) into x
  36.   put y - the scroll of card field 1 into y
  37.   put x into item 1 of r
  38.   put x+theLength*ts+5 into item 3 of r
  39.   put y - round(th/2) into item 2 of r
  40.   put y + round(th/2) into item 4 of r
  41.   set the rect of btn "foundit" to r
  42.   set the hilite of btn "foundit" to true
  43.   wait for 1 seconds
  44. end showIt
  45.  
  46.  
  47. -- part 5 (field)
  48. -- low flags: 01
  49. -- high flags: 2007
  50. -- rect: left=18 top=32 right=285 bottom=384
  51. -- title width / last selected line: 0
  52. -- icon id / first selected line: 0 / 0
  53. -- text alignment: 0
  54. -- font id: 22
  55. -- text size: 10
  56. -- style flags: 0
  57. -- line height: 13
  58. -- part name: Documentation
  59.  
  60.  
  61. -- part 6 (button)
  62. -- low flags: 00
  63. -- high flags: 8003
  64. -- rect: left=299 top=298 right=320 bottom=438
  65. -- title width / last selected line: 0
  66. -- icon id / first selected line: 0 / 0
  67. -- text alignment: 1
  68. -- font id: 0
  69. -- text size: 12
  70. -- style flags: 0
  71. -- line height: 16
  72. -- part name: Show LSC Source
  73. ----- HyperTalk script -----
  74. on mouseUp
  75.   get the visible of card field "source"
  76.   set the visible of card field "source" to not it
  77.   if it is false then
  78.     set the name of me to "Hide LSC Source"
  79.   else
  80.     set the name of me to "Show LSC Source"
  81.   end if
  82. end mouseUp
  83.  
  84.  
  85.  
  86. -- part 9 (field)
  87. -- low flags: 00
  88. -- high flags: 0004
  89. -- rect: left=389 top=268 right=289 bottom=483
  90. -- title width / last selected line: 0
  91. -- icon id / first selected line: 0 / 0
  92. -- text alignment: 0
  93. -- font id: 3
  94. -- text size: 12
  95. -- style flags: 0
  96. -- line height: 16
  97. -- part name: found
  98.  
  99.  
  100. -- part 10 (button)
  101. -- low flags: 00
  102. -- high flags: 2000
  103. -- rect: left=395 top=76 right=111 bottom=480
  104. -- title width / last selected line: 0
  105. -- icon id / first selected line: 0 / 0
  106. -- text alignment: 1
  107. -- font id: 0
  108. -- text size: 12
  109. -- style flags: 0
  110. -- line height: 16
  111. -- part name: Not case
  112. ----- HyperTalk script -----
  113. on mouseUp
  114.   ask "Find what?"
  115.   put it into myTarget
  116.   if myTarget is not empty then
  117.     set the scroll of card field 1 to 0
  118.     get FindInField(card field 1,myTarget,"true",0)
  119.     put it into card field "found"
  120.     showIt it,the number of chars in myTarget
  121.   end if
  122. end mouseUp
  123.  
  124.  
  125.  
  126. -- part 11 (button)
  127. -- low flags: 00
  128. -- high flags: 2000
  129. -- rect: left=395 top=123 right=158 bottom=480
  130. -- title width / last selected line: 0
  131. -- icon id / first selected line: 0 / 0
  132. -- text alignment: 1
  133. -- font id: 0
  134. -- text size: 12
  135. -- style flags: 0
  136. -- line height: 16
  137. -- part name: New Button
  138. ----- HyperTalk script -----
  139. on mouseUp
  140.   ask "Find what?"
  141.   put it into myTarget
  142.   if myTarget is not empty then
  143.     set the scroll of card field 1 to 0
  144.     get FindInField(card field 1,myTarget,"",0)
  145.     put it into card field "found"
  146.     showIt it,the number of chars in myTarget
  147.   end if
  148. end mouseUp
  149.  
  150.  
  151.  
  152. -- part 12 (button)
  153. -- low flags: 00
  154. -- high flags: 2000
  155. -- rect: left=395 top=169 right=215 bottom=480
  156. -- title width / last selected line: 0
  157. -- icon id / first selected line: 0 / 0
  158. -- text alignment: 1
  159. -- font id: 0
  160. -- text size: 12
  161. -- style flags: 0
  162. -- line height: 16
  163. -- part name: New Button
  164. ----- HyperTalk script -----
  165. on mouseUp
  166.   ask "Show what?"
  167.   if it is not empty then
  168.     hide message
  169.     set the scroll of card field 1 to 0
  170.     put it into target
  171.     put false into done
  172.     put 0 into start
  173.     --put empty into msg
  174.     repeat while done is false
  175.       get FindInField(card field 1,target,"",start)
  176.       if item 1 of it is 0 then
  177.         put true into done
  178.       else
  179.         put it into card field "found"
  180.         showIt it,the number of chars in target
  181.         put item 1 of it + 1 into start
  182.       end if
  183.     end repeat
  184.   end if
  185. end mouseUp
  186.  
  187.  
  188.  
  189.  
  190. -- part 14 (button)
  191. -- low flags: 00
  192. -- high flags: 0000
  193. -- rect: left=395 top=46 right=64 bottom=468
  194. -- title width / last selected line: 0
  195. -- icon id / first selected line: 0 / 0
  196. -- text alignment: 1
  197. -- font id: 0
  198. -- text size: 12
  199. -- style flags: 0
  200. -- line height: 16
  201. -- part name: New Button
  202.  
  203.  
  204. -- part 7 (field)
  205. -- low flags: 81
  206. -- high flags: 0007
  207. -- rect: left=18 top=31 right=290 bottom=489
  208. -- title width / last selected line: 0
  209. -- icon id / first selected line: 0 / 0
  210. -- text alignment: 0
  211. -- font id: 3
  212. -- text size: 10
  213. -- style flags: 0
  214. -- line height: 13
  215. -- part name: Source
  216.  
  217.  
  218. -- part 16 (button)
  219. -- low flags: 00
  220. -- high flags: 0000
  221. -- rect: left=228 top=91 right=103 bottom=251
  222. -- title width / last selected line: 0
  223. -- icon id / first selected line: 0 / 0
  224. -- text alignment: 1
  225. -- font id: 0
  226. -- text size: 12
  227. -- style flags: 0
  228. -- line height: 16
  229. -- part name: foundIt
  230.  
  231.  
  232. -- part contents for card part 5
  233. ----- text -----
  234. FindInField version 1.4
  235. Roger Brown
  236.  
  237. The FindInField XFCN locates a string in a given field 
  238. or container as fast as HyperCard can.
  239.  
  240.  It searches only the specified container.  
  241.  
  242. You can specify a starting position and whether or not 
  243. the search is case sensitive.
  244.  
  245.  It returns information on the location of the find: 
  246.     item 1 is the byte position in the string (1-based)
  247.     item 2 is the line number in the string
  248.     item 3 is the byte position in the line (1-based)
  249.     item 4 is the item number in the line
  250.       --- all are zero if the string is not found
  251.    
  252.  
  253. INVOKING FindInField
  254.  
  255.   get FindInField(field,target,"true",start)
  256.  
  257.      where   field   is the name of the field or          
  258.                      container to search
  259.              target  is the string to search for
  260.             "true"   is an option parameter to indicate                 
  261.                      case sensitivity for the search
  262.              start   is the (optional) starting byte 
  263.                      offset
  264.                    
  265.  
  266. EXAMPLE
  267.  
  268.  ex.  get FindInField(card field 1,"EXAMPLE","true",0)
  269.  
  270.  
  271. REVISION HISTORY
  272. 1.1  4/31/88 - FindInField using HCLib was way too slow
  273. 1.2  5/5/88  - rewrite in more "C" style, one pass over 
  274.                the field
  275. 1.3  5/10/88 - add case sensitivity and starting 
  276.                position
  277. 1.4 7/21/88  - add item number output
  278.  
  279. -- part contents for card part 7
  280. ----- text -----
  281. /* FindInField1.4.c */
  282. /* ┬⌐ Trustees of Dartmouth College */
  283. /* written in LightSpeed C  ┬⌐ Think Technologies, Inc */
  284. /* by Roger Brown 7/21/88  Courseware Development group */
  285.  
  286. /* version 1.4 2/289 returns item number in line */
  287.  
  288. /* add start byte input as last parameter */
  289.  
  290. /* This is a HyperCard XFCN that locates a target string in an input
  291.    field (or other container) and returns the byte position in the field, the number 
  292.    of the line where it was find, and the byte position in that line.
  293.    Case sensitivity is used if an optional third parameter is "true".
  294.    An optional fourth parameter can be sent to specify the byte position
  295.    in the field where the search should start.
  296.     
  297.    Syntax is:
  298.    
  299.         get FindInField(container,target,"true",startByte)
  300.    
  301.    ex.      get FindInField(card field 1,"***")
  302.   
  303.    returns: item 1: byte offset in field or 0 if not found
  304.             item 2: line of card field 1 that contains '***' or 0
  305.             item 3: byte position in the line or 0
  306.             item 4: item number that contains it or 0
  307.    
  308.    where    container   is any hypercard container (field, variable), presumed to be 
  309.                         multi-lined.
  310.             target      is the string to look for   
  311.             true        is an optional parameter turning on case sensitivity   
  312.             startByte   is an optional parameter specifying the byte position
  313.                         in the container where the search should start.
  314.                                
  315.       To compile: create a project with this and MacTraps. Build as code resource type
  316.                   XFCN named FindInField.
  317. */
  318.  
  319.  
  320.  
  321. #include "stddata_ctype.c"
  322. #include "strings.c"
  323. #include "ToolboxUtil.h"
  324. #include "HyperXCmd.h"
  325. #include "XCmdGlue.inc.c"
  326. #include "SetUpA4.h"
  327.  
  328. /* change a string to all upper case */
  329.  
  330. int toupper(c)
  331. char    c;
  332. {
  333.     return( (c>='a')&&(c<='z') ? (c-('a'-'A')) : c );
  334. }
  335.  
  336. /* see if two characters match, maybe ignoring case */
  337.  
  338. matchChar(a,b,theCase)
  339. char a,b;
  340. int theCase;
  341. {
  342.     if (theCase==1) return (a==b);
  343.     return (toupper(a)==toupper(b));
  344. }
  345.  
  346. /* build the return result string from numeric parts */
  347.  
  348. BuildResult(fPos,fLine,lPos,item,result)
  349. int fPos,fLine,lPos,item;
  350. char *result;
  351. {
  352.     char temp[64];
  353.     
  354.     NumToString((long)fPos,temp);
  355.     PtoCstr((char*)temp);
  356.     strcpy(result,temp);
  357.     strcat(result,",");
  358.     NumToString((long)fLine,temp);
  359.     PtoCstr((char*)temp);
  360.     strcat(result,temp);
  361.     strcat(result,",");
  362.     NumToString((long)lPos,temp);
  363.     PtoCstr((char*)temp);
  364.     strcat(result,temp);
  365.     strcat(result,",");
  366.     NumToString((long)item,temp);
  367.     PtoCstr((char*)temp);
  368.     strcat(result,temp);
  369. }
  370.  
  371. /* find the target as specified */
  372.  
  373. FindIt(theField,theTarget,caseSens,result,start)
  374. char *theField;          /* pointer to field to search */
  375. char *theTarget;         /* pointer to target string */
  376. char *result;            /* pointer to the return result string */
  377. int caseSens,start;      /* case sensitive = 1 for yes, start is where (byte) to start */
  378. {
  379.     int c;             /* index of current character */
  380.     int tLen;          /* target length */
  381.     int line;          /* line currently being searched */
  382.     int lPos;          /* character position in line */
  383.     int item;          /* item number in line */
  384.     register char *fc,*tc; /* pointers to field and target characters */
  385.     
  386.     tLen = strlen(theTarget);         /* assume this is short */
  387.     c = lPos = 0; 
  388.     line = item = 1;                            /* offset into theField */
  389.     fc = theField;                       /* pointer into theField */
  390.     while (*fc != 0) {                   /* scan to end */
  391.         tc = theTarget;                  /* pointer to theTarget */
  392.         if (*fc==13) {
  393.              line++;                     /* passing a line boundary */
  394.              lPos = -1;                  /* reset position */
  395.              item = 1;                   /* reset item */
  396.         }
  397.         if (*fc==44) item++;
  398.         if ((c+1) > start) {             /* don't go until something matches */
  399.             while (matchChar(*tc,*fc,caseSens)) {   /* check for match */
  400.                 fc++;                      /* cycle through matching characters */
  401.                 tc++;
  402.                 if (tc == (theTarget+tLen)){   /* all target characters matched */
  403.                     BuildResult(c+1,line,lPos+1,item,result);
  404.                      return;  /* matched */
  405.                 }
  406.             }
  407.         }
  408.         c++;                          /* move along */
  409.         lPos++;
  410.         fc = theField + c;            /* reset pointer after a search */
  411.     }
  412.     BuildResult(0,0,0,0,result);
  413.     return;                         /* target not found */
  414. }
  415.  
  416. /* XCMD entry */
  417.  
  418. pascal void FindInField(paramPtr)
  419. XCmdBlockPtr    paramPtr;
  420. {   
  421.     int i;
  422.     Str255 theResult,caseStr,startStr;
  423.     Ptr theField,theTarget,casePtr,theStart;
  424.     long line;
  425.     Size len;
  426.     CursHandle theCursor;
  427.     Handle resultHandle;
  428.     int theCase;
  429.     long startAt;
  430.     
  431.     /* put up a watch */
  432.     
  433.     theCursor = GetCursor(watchCursor);
  434.     SetCursor(*theCursor);
  435.     
  436.     /* lock parameters down so we can point to themn */
  437.     
  438.     for (i=0;i<paramPtr->paramCount;i++) {
  439.         MoveHHi(paramPtr->params[i]);
  440.         HLock(paramPtr->params[i]);
  441.     }
  442.     
  443.     /* get the parameters */
  444.     
  445.     theField = *(paramPtr->params[0]);   /* the container */
  446.     theTarget = *(paramPtr->params[1]);  /* the target string */
  447.     if (paramPtr->paramCount>2)  {       /* case sensitivity flag */
  448.         casePtr = *(paramPtr->params[2]);
  449.         strcpy(caseStr,casePtr);
  450.     }
  451.     else strcpy(caseStr,"");
  452.     for (i=0;i<strlen(caseStr);i++) caseStr[i] = toupper(caseStr[i]);
  453.     if (strcmp(caseStr,"TRUE")==0) theCase = 1;
  454.     else theCase = 0;
  455.     
  456.     if (paramPtr->paramCount>3) {        /* start byte */
  457.         theStart = *(paramPtr->params[3]);
  458.         strcpy(startStr,theStart);
  459.         CtoPstr((char *)startStr);
  460.         StringToNum(startStr,&startAt);
  461.     }
  462.     else startAt = 0;
  463.        
  464.     /* do it */
  465.     
  466.     FindIt(theField,theTarget,theCase,theResult,(int)startAt);
  467.     
  468.     /* assemble the return string */
  469.     
  470.     len = 1+strlen(theResult);
  471.     resultHandle = NewHandle(len);
  472.     HLock(resultHandle);
  473.     BlockMove(theResult,*resultHandle,len);
  474.     HUnlock(resultHandle);
  475.     paramPtr->returnValue = resultHandle;
  476.     for (i=0;i<paramPtr->paramCount;i++) HUnlock (paramPtr->params[i]);
  477.     return;
  478. }
  479.  
  480. pascal void main(paramPtr)
  481. XCmdBlockPtr    paramPtr;
  482. /* this is the entry point for the XFCN */
  483. {
  484.         RememberA0();
  485.         SetUpA4();                  /* to access globals */
  486.         FindInField(paramPtr);      /* run the main event loop */  
  487.         RestoreA4(); 
  488.         return;
  489. }   
  490.  
  491.  
  492.  
  493. -- part contents for card part 9
  494. ----- text -----
  495. 128,5,35,1